package com.masterclient.app.modules.cart;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.masterclient.app.base.ProgressLoginDialog;
import com.masterclient.app.modules.cart.adapter.ShopCartAdapter;
import com.masterclient.app.modules.cart.beans.DealerProductsBean;
import com.masterclient.app.modules.cart.beans.NormalProductsItemBean;
import com.masterclient.app.modules.cart.beans.ProductsItemBean;
import com.masterclient.app.modules.cart.beans.PropertiesBean;
import com.masterclient.app.modules.cart.beans.SellerItemBean;
import com.masterclient.app.modules.cart.beans.ShopCartBean;
import com.masterclient.app.modules.cart.beans.ShopCartCustomItemBean;
import com.masterclient.app.modules.cart.contract.ShopCartPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShopCartDoDataUtil {

    private boolean isCheckAll = false;
    private List<DealerProductsBean> dealerProductsBeanList = new ArrayList<>();

    /**
     * 处理数据组装
     */
    List<ShopCartCustomItemBean> doShopCartApiDataToModel(ShopCartBean shopCartBean, List<ProductsItemBean> productsList,
                                                          TextView tvEdit, LinearLayout rlBottom) {
        List<ShopCartCustomItemBean> shopCartCustomItemBeanList = new ArrayList<>();
        if (shopCartBean.data == null) {
            return shopCartCustomItemBeanList;
        }

        if ((shopCartBean.data.normalProducts == null || shopCartBean.data.normalProducts.size() == 0) &&
                (shopCartBean.data.expiredProducts == null || shopCartBean.data.expiredProducts.size() == 0)) {
            //空视图信息
            ShopCartCustomItemBean shopCartCustomItemBean = new ShopCartCustomItemBean();
            shopCartCustomItemBean.type = ShopCartCustomItemBean.NULL;
            shopCartCustomItemBeanList.add(shopCartCustomItemBean);
//            tvEdit.setVisibility(View.GONE);
//            rlBottom.setVisibility(View.GONE);
        } else {
            //店铺、商品信息(普通)
            doShopCartGoodsData(shopCartBean.data.normalProducts, shopCartCustomItemBeanList, productsList);
            //失效商品信息
            if (shopCartBean.data.expiredProducts != null && shopCartBean.data.expiredProducts.size() > 0) {
                //失效商品Head信息
                ShopCartCustomItemBean shopCartCustomItemBean = new ShopCartCustomItemBean();
                shopCartCustomItemBean.type = ShopCartCustomItemBean.DISABLED_HEAD;
                shopCartCustomItemBean.disableNum = shopCartBean.data.expiredProducts.size() + "";
                shopCartCustomItemBeanList.add(shopCartCustomItemBean);
                int originalPosition = 0;
                //失效商品信息
                for (ProductsItemBean productsItemBean : shopCartBean.data.expiredProducts) {
                    ShopCartCustomItemBean mShopCartCustomItemBean = new ShopCartCustomItemBean();
                    mShopCartCustomItemBean.itemData = productsItemBean;
                    mShopCartCustomItemBean.type = ShopCartCustomItemBean.DISABLED;
                    mShopCartCustomItemBean.originalPosition = originalPosition;
                    shopCartCustomItemBeanList.add(mShopCartCustomItemBean);
                    ++originalPosition;
                }
            }

            //满减活动商品重新组合
            for (int i = 0; i < productsList.size(); i++) {
                if ((i + 1) < productsList.size() && productsList.get(i).splitSuffix.equals(productsList.get(i + 1).splitSuffix)) {
                    if (!TextUtils.isEmpty(productsList.get(i).activityId)) {
                        productsList.get(i).isShowAllLine = false;
                        productsList.get(i).isShowHalfLine = true;
                        //商品列表中活动信息显示
                        if ((i + 1) < productsList.size() &&
                                !TextUtils.isEmpty(productsList.get(i).activityId) &&
                                !TextUtils.isEmpty(productsList.get(i + 1).activityId) &&
                                productsList.get(i).activityId.equals(productsList.get(i + 1).activityId)) {
                            if (i > 0 &&
                                    !TextUtils.isEmpty(productsList.get(i).activityId) &&
                                    !TextUtils.isEmpty(productsList.get(i - 1).activityId) &&
                                    productsList.get(i).activityId.equals(productsList.get(i - 1).activityId)) {
                                productsList.get(i).isShowAllLine = true;
                                productsList.get(i).isShowHalfLine = false;
                                productsList.get(i).isShowActivity = false;
                            } else {
                                productsList.get(i).isShowAllLine = true;
                                productsList.get(i).isShowHalfLine = false;
                                productsList.get(i).isShowActivity = true;
                            }
                            productsList.get(i + 1).isShowAllLine = false;
                            productsList.get(i + 1).isShowHalfLine = true;
                            productsList.get(i + 1).isShowActivity = false;
                        }
                    }
                }
            }

            //将重新组合好的满减商品重新复制
            for (int i = 0; i < productsList.size(); i++) {
                for (int j = 0; j < shopCartCustomItemBeanList.size(); j++) {
                    if (shopCartCustomItemBeanList.get(j).type.equals(ShopCartCustomItemBean.GOODS)) {
                        ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBeanList.get(j).itemData;
                        if (productsList.get(i).productId.equals(productsItemBean.productId)) {
                            shopCartCustomItemBeanList.get(j).itemData = productsList.get(i);
                        }
                    }
                }
            }

            //处理相邻相同的活动信息头部展示
            for (int i = 0; i < shopCartCustomItemBeanList.size(); i++) {
                if (shopCartCustomItemBeanList.get(i).type.equals(ShopCartCustomItemBean.ACTIVITY)) {
                    ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBeanList.get(i).itemOtherData;
                    if (!productsItemBean.isShowActivity) {
                        shopCartCustomItemBeanList.remove(i);
                    }
                }
            }

            tvEdit.setVisibility(View.VISIBLE);
            rlBottom.setVisibility(View.VISIBLE);
        }
        //推荐商品信息
        if (shopCartBean.data.recommendProducts != null && shopCartBean.data.recommendProducts.size() > 0) {
            ShopCartCustomItemBean shopCartCustomItemBean = new ShopCartCustomItemBean();
            shopCartCustomItemBean.itemData = shopCartBean.data.recommendProducts;
            shopCartCustomItemBean.type = ShopCartCustomItemBean.RECOMMEND;
            shopCartCustomItemBeanList.add(shopCartCustomItemBean);
        }
        return shopCartCustomItemBeanList;
    }

    /**
     * 处理店铺、商品信息
     */
    private void doShopCartGoodsData(List<NormalProductsItemBean> shopCartBean,
                                     List<ShopCartCustomItemBean> shopCartCustomItemBeanList, List<ProductsItemBean> productsList) {
        //店铺、商品信息
        if (shopCartBean != null && shopCartBean.size() > 0) {
            for (int h = 0; h < shopCartBean.size(); h++) {
                NormalProductsItemBean normalProductsItemBean = shopCartBean.get(h);
                if (normalProductsItemBean != null && normalProductsItemBean.products != null &&
                        normalProductsItemBean.products.size() > 0) {

                    //店铺信息
                    ShopCartCustomItemBean shopCartCustomItemBean = new ShopCartCustomItemBean();
                    shopCartCustomItemBean.type = ShopCartCustomItemBean.SHOP;
                    shopCartCustomItemBean.originalPosition = h;
                    if (normalProductsItemBean.promotions != null && normalProductsItemBean.promotions.size() > 0) {
                        normalProductsItemBean.seller.isActivity = true;
                    }
                    shopCartCustomItemBean.itemData = normalProductsItemBean.seller;
                    shopCartCustomItemBeanList.add(shopCartCustomItemBean);

                    //商品信息(有活动的先加入)
                    for (int i = 0; i < normalProductsItemBean.products.size(); i++) {
                        //活动信息
                        for (int j = 0; j < normalProductsItemBean.promotions.size(); j++) {
                            if (normalProductsItemBean.promotions.get(j).activityId.equals(normalProductsItemBean.products.get(i).activityId)) {
                                ShopCartCustomItemBean shopCartCustomItemBean1 = new ShopCartCustomItemBean();
                                shopCartCustomItemBean1.itemData = normalProductsItemBean.promotions.get(j);
                                shopCartCustomItemBean1.itemOtherData = normalProductsItemBean.products.get(i);
                                shopCartCustomItemBean1.type = ShopCartCustomItemBean.ACTIVITY;
                                shopCartCustomItemBeanList.add(shopCartCustomItemBean1);
                            }
                        }

                        if (!TextUtils.isEmpty(normalProductsItemBean.products.get(i).activityId)) {
                            normalProductsItemBean.products.get(i).isShowAllLine = false;
                            normalProductsItemBean.products.get(i).isShowHalfLine = true;
                            //商品列表中活动信息显示
                            if ((i + 1) < normalProductsItemBean.products.size() &&
                                    !TextUtils.isEmpty(normalProductsItemBean.products.get(i).activityId) &&
                                    !TextUtils.isEmpty(normalProductsItemBean.products.get(i + 1).activityId) &&
                                    normalProductsItemBean.products.get(i).activityId.equals(normalProductsItemBean.products.get(i + 1).activityId)) {
                                if (i > 0 && !TextUtils.isEmpty(normalProductsItemBean.products.get(i).activityId) &&
                                        !TextUtils.isEmpty(normalProductsItemBean.products.get(i - 1).activityId) &&
                                        normalProductsItemBean.products.get(i).activityId.equals(normalProductsItemBean.products.get(i - 1).activityId)) {
                                    normalProductsItemBean.products.get(i).isShowAllLine = true;
                                    normalProductsItemBean.products.get(i).isShowHalfLine = false;
                                    normalProductsItemBean.products.get(i).isShowActivity = false;
                                } else {
                                    normalProductsItemBean.products.get(i).isShowAllLine = true;
                                    normalProductsItemBean.products.get(i).isShowHalfLine = false;
                                    normalProductsItemBean.products.get(i).isShowActivity = true;
                                }
                                normalProductsItemBean.products.get(i + 1).isShowAllLine = false;
                                normalProductsItemBean.products.get(i + 1).isShowHalfLine = true;
                                normalProductsItemBean.products.get(i + 1).isShowActivity = false;
                            }

                            //店铺第一个商品无分割线
                            normalProductsItemBean.products.get(i).showLine = i != 0;
                            //商品是否可勾选
                            normalProductsItemBean.products.get(i).isUsable = Integer.parseInt(normalProductsItemBean.products.get(i).stock) >= Integer.parseInt(normalProductsItemBean.products.get(i).num);
                            normalProductsItemBean.products.get(i).sellerId = normalProductsItemBean.seller.sellerId;
                            normalProductsItemBean.products.get(i).splitSuffix = normalProductsItemBean.seller.splitSuffix;
                            ShopCartCustomItemBean shopCartCustomItemBean2 = new ShopCartCustomItemBean();
                            shopCartCustomItemBean2.itemData = normalProductsItemBean.products.get(i);
                            shopCartCustomItemBean2.type = ShopCartCustomItemBean.GOODS;
                            shopCartCustomItemBean2.originalPosition = i;
                            shopCartCustomItemBeanList.add(shopCartCustomItemBean2);
                            productsList.add(normalProductsItemBean.products.get(i));
                        }
                    }

                    //商品信息(无活动的后加入)
                    for (int i = 0; i < normalProductsItemBean.products.size(); i++) {
                        if (TextUtils.isEmpty(normalProductsItemBean.products.get(i).activityId)) {
                            normalProductsItemBean.products.get(i).isShowAllLine = false;
                            normalProductsItemBean.products.get(i).isShowHalfLine = false;

                            //店铺第一个商品无分割线
                            normalProductsItemBean.products.get(i).showLine = i != 0;
                            //商品是否可勾选
                            normalProductsItemBean.products.get(i).isUsable = Integer.parseInt(normalProductsItemBean.products.get(i).stock) >= Integer.parseInt(normalProductsItemBean.products.get(i).num);
                            normalProductsItemBean.products.get(i).sellerId = normalProductsItemBean.seller.sellerId;
                            normalProductsItemBean.products.get(i).splitSuffix = normalProductsItemBean.seller.splitSuffix;
                            ShopCartCustomItemBean shopCartCustomItemBean2 = new ShopCartCustomItemBean();
                            shopCartCustomItemBean2.itemData = normalProductsItemBean.products.get(i);
                            shopCartCustomItemBean2.type = ShopCartCustomItemBean.GOODS;
                            shopCartCustomItemBean2.originalPosition = i;
                            shopCartCustomItemBeanList.add(shopCartCustomItemBean2);
                            productsList.add(normalProductsItemBean.products.get(i));
                        }
                    }

                    //店铺是否可勾选
                    for (int i = 0; i < normalProductsItemBean.products.size(); i++) {
                        if (normalProductsItemBean.seller.splitSuffix.equals(normalProductsItemBean.products.get(i).splitSuffix)) {
                            if (normalProductsItemBean.products.get(i).isUsable) {
                                normalProductsItemBean.seller.isUsable = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 更新购物袋数据后，处理（塞入）之前选中记录
     */
    void doShopCartSelected(List<String> cardLists, List<ShopCartCustomItemBean> mShopCartCustomItemBean) {
        if (cardLists == null || cardLists.size() <= 0) {
            return;
        }
        //商品处理
        for (String cardId : cardLists) {
            for (int i = 0; i < mShopCartCustomItemBean.size(); i++) {
                if (ShopCartCustomItemBean.GOODS.equals(mShopCartCustomItemBean.get(i).type)) {
                    ProductsItemBean productsItemBean = (ProductsItemBean) mShopCartCustomItemBean.get(i).itemData;
                    if (cardId.equals(productsItemBean.cartId)) {
                        productsItemBean.isCheck = true;
                    }
                }
            }
        }
        //店铺处理
        for (int i = 0; i < mShopCartCustomItemBean.size(); i++) {
            if (ShopCartCustomItemBean.SHOP.equals(mShopCartCustomItemBean.get(i).type)) {
                SellerItemBean sellerItemBean = (SellerItemBean) mShopCartCustomItemBean.get(i).itemData;
                for (int j = 0; j < mShopCartCustomItemBean.size(); j++) {
                    if (ShopCartCustomItemBean.GOODS.equals(mShopCartCustomItemBean.get(j).type)) {
                        ProductsItemBean productsItemBean = (ProductsItemBean) mShopCartCustomItemBean.get(j).itemData;
                        if (sellerItemBean.splitSuffix.equals(productsItemBean.splitSuffix)) {
                            if (!productsItemBean.isCheck) {
                                sellerItemBean.isCheck = false;
                                break;
                            } else {
                                sellerItemBean.isCheck = true;
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 获取勾选商品id ，以","拼接”的字符数
     */
    String getSelectedCartIds(List<ShopCartCustomItemBean> mShopCartCustomItemBean) {
        StringBuilder cartIds = new StringBuilder();
        if (mShopCartCustomItemBean != null && mShopCartCustomItemBean.size() > 0) {
            for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
                if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBean.type)) {
                    ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBean.itemData;
                    if (productsItemBean.isUsable && productsItemBean.isCheck) {
                        cartIds.append(productsItemBean.cartId).append(",");
                    }
                }
            }
        }
        if (!TextUtils.isEmpty(cartIds.toString()) && cartIds.toString().contains(",")) {
            cartIds = new StringBuilder(cartIds.substring(0, cartIds.length() - 1));
        }
        return cartIds.toString();
    }

    /**
     * 获取失效商品id ，以","拼接”的字符数
     */
    String getDisableGoodsCartIds(List<ShopCartCustomItemBean> mShopCartCustomItemBean) {
        StringBuilder cartIds = new StringBuilder();
        if (mShopCartCustomItemBean != null && mShopCartCustomItemBean.size() > 0) {
            for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
                if (ShopCartCustomItemBean.DISABLED.equals(shopCartCustomItemBean.type)) {
                    ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBean.itemData;
                    cartIds.append(productsItemBean.cartId).append(",");
                }
            }
        }
        if (!TextUtils.isEmpty(cartIds.toString()) && cartIds.toString().contains(",")) {
            cartIds = new StringBuilder(cartIds.substring(0, cartIds.length() - 1));
        }
        return cartIds.toString();
    }

    /**
     * 获取勾选商品id列表
     */
    List<String> getSelectedCartIdList(List<ShopCartCustomItemBean> mShopCartCustomItemBean) {
        List<String> lists = new ArrayList<>();
        if (mShopCartCustomItemBean != null) {
            for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
                if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBean.type)) {
                    ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBean.itemData;
                    if (productsItemBean.isCheck) {
                        lists.add(productsItemBean.cartId);
                    }
                }
            }
        }
        return lists;
    }

    /**
     * 获取勾选商品id & 数量
     * isEvent 埋点使用
     */
    String getBuyProduct(boolean isEvent, List<ShopCartCustomItemBean> mShopCartCustomItemBean) {
        JSONObject buyProductsInfo = new JSONObject();
        try {
            if (mShopCartCustomItemBean != null) {
                for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
                    if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBean.type)) {
                        ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBean.itemData;
                        if (productsItemBean.isUsable && productsItemBean.isCheck) {
                            String specification = "";
                            for (PropertiesBean properties : productsItemBean.properties) {
                                specification += properties.aliasName + "";
                            }
                            buyProductsInfo.put(productsItemBean.productId, Integer.parseInt(productsItemBean.num));
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return buyProductsInfo.toString();
    }

    /**
     * 获取勾选商品id & 数量
     */
    public Map<String, Integer> getBuyProduct2(List<ShopCartCustomItemBean> mShopCartCustomItemBean) {
        Map<String, Integer> buyProductsInfo = new HashMap<>();
        if (mShopCartCustomItemBean != null) {
            for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
                if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBean.type)) {
                    ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBean.itemData;
                    if (productsItemBean.isUsable && productsItemBean.isCheck) {
                        buyProductsInfo.put("buy_product[" + Integer.parseInt(productsItemBean.productId) + "]", Integer.parseInt(productsItemBean.num));
                    }
                }
            }
        }
        return buyProductsInfo;
    }

    /**
     * 获取勾选店铺id列表
     */
    public List<String> getSelectedShopIdList(List<ShopCartCustomItemBean> mShopCartCustomItemBean) {
        List<String> lists = new ArrayList<>();
        if (mShopCartCustomItemBean != null) {
            for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
                if (ShopCartCustomItemBean.SHOP.equals(shopCartCustomItemBean.type)) {
                    SellerItemBean sellerItemBean = (SellerItemBean) shopCartCustomItemBean.itemData;
                    if (sellerItemBean.isUsable && sellerItemBean.isCheck) {
                        lists.add(sellerItemBean.sellerId);
                    }
                }
            }
        }
        return lists;
    }

    /**
     * 处理店铺选中状态
     */
    void doShopCheckStatus(String shopId, boolean isCheck, List<ShopCartCustomItemBean> mShopCartCustomItemBean,
                           CheckBox cbCheckAll, ShopCartAdapter shopCartAdapter) {
        //店铺处理
        for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
            if (ShopCartCustomItemBean.SHOP.equals(shopCartCustomItemBean.type)) {
                SellerItemBean sellerItemBean = (SellerItemBean) shopCartCustomItemBean.itemData;
                if (shopId.equals(sellerItemBean.splitSuffix)) {
                    ((SellerItemBean) shopCartCustomItemBean.itemData).isCheck = isCheck;
                }
            }
        }

        //商品处理
        for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
            if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBean.type)) {
                ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBean.itemData;
                if (shopId.equals(productsItemBean.splitSuffix)) {
                    ((ProductsItemBean) shopCartCustomItemBean.itemData).isCheck = isCheck;
                }
            }
        }
        showCheckAll(mShopCartCustomItemBean, cbCheckAll, shopCartAdapter);
    }

    /**
     * 处理商品选中状态
     */
    void doGoodCheckStatus(String shopId, String productId, boolean isCheck, List<ShopCartCustomItemBean> mShopCartCustomItemBean,
                           CheckBox cbCheckAll, ShopCartAdapter shopCartAdapter) {
        boolean isAllCheck = true;
        //商品处理
        for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
            if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBean.type)) {
                ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBean.itemData;
                if (shopId.equals(productsItemBean.splitSuffix) && productId.equals(productsItemBean.productId)) {
                    productsItemBean.isCheck = isCheck;
                }
                if (!productsItemBean.isCheck && shopId.equals(productsItemBean.splitSuffix)) {
                    //当前商品的店铺下，有一个商品没选中就表示店铺没选中
                    isAllCheck = false;
                }
            }
        }

        //店铺处理
        for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
            if (ShopCartCustomItemBean.SHOP.equals(shopCartCustomItemBean.type)) {
                SellerItemBean sellerItemBean = (SellerItemBean) shopCartCustomItemBean.itemData;
                if (shopId.equals(sellerItemBean.splitSuffix)) {
                    if (!isCheck) {
                        //商品没选择，也得去掉店铺选中
                        sellerItemBean.isCheck = false;
                        break;
                    } else {
                        sellerItemBean.isCheck = isAllCheck;
                    }
                }
            }
        }

        showCheckAll(mShopCartCustomItemBean, cbCheckAll, shopCartAdapter);
    }

    /**
     * 处理全选选中状态
     */
    void doCheckAll(boolean isCheck, List<ShopCartCustomItemBean> mShopCartCustomItemBean,
                    CheckBox cbCheckAll, ShopCartAdapter shopCartAdapter, ShopCartPresenter mPresenter,
                    ProgressLoginDialog progressLoginDialog) {
        //店铺、商品处理
        if (mShopCartCustomItemBean != null && mShopCartCustomItemBean.size() > 0) {
            for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
                if (ShopCartCustomItemBean.SHOP.equals(shopCartCustomItemBean.type)) {
                    ((SellerItemBean) shopCartCustomItemBean.itemData).isCheck = isCheck;
                }
                if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBean.type)) {
                    ((ProductsItemBean) shopCartCustomItemBean.itemData).isCheck = isCheck;
                }
            }
            showCheckAll(mShopCartCustomItemBean, cbCheckAll, shopCartAdapter);
            mPresenter.getShopCartList(getSelectedCartIds(mShopCartCustomItemBean), progressLoginDialog);
        }
    }

    /**
     * 判断是否已经全选
     */
    void showCheckAll(List<ShopCartCustomItemBean> mShopCartCustomItemBean,
                      CheckBox cbCheckAll, ShopCartAdapter shopCartAdapter) {
        boolean checkAll = true;
        boolean isAllUseLess = true;
        isCheckAll = true;
        //检索是否全是不可用商品
        for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
            if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBean.type)) {
                ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBean.itemData;
                if (productsItemBean.isUsable) {
                    isAllUseLess = false;
                    break;
                }
            }
        }

        boolean hasGoods = false;
        for (ShopCartCustomItemBean shopCartCustomItemBean : mShopCartCustomItemBean) {
            if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBean.type)) {
                hasGoods = true;
            }
            if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBean.type)) {
                ProductsItemBean productsItemBean = (ProductsItemBean) shopCartCustomItemBean.itemData;
                if (isAllUseLess) {
                    if (!productsItemBean.isCheck) {
                        checkAll = false;
                        isCheckAll = false;
                        break;
                    }
                } else {
                    if (productsItemBean.isUsable && !productsItemBean.isCheck) {
                        checkAll = false;
                        isCheckAll = false;
                        break;
                    }
                }
            }
        }

        if (!hasGoods){
            checkAll = false;
        }
        cbCheckAll.setChecked(checkAll);
        shopCartAdapter.setData(mShopCartCustomItemBean);
    }

    boolean isCheckAll() {
        return isCheckAll;
    }

    List<DealerProductsBean> getConfirmPopData(ShopCartBean shopCartBean) {
        if (shopCartBean != null && shopCartBean.data != null && shopCartBean.data.confirmPopInfo != null) {
            dealerProductsBeanList.clear();
            //经销商
            if (shopCartBean.data.confirmPopInfo.dealerProducts != null &&
                    shopCartBean.data.confirmPopInfo.dealerProducts.size() > 0) {
                for (int i = 0; i < shopCartBean.data.confirmPopInfo.dealerProducts.size(); i++) {
                    DealerProductsBean dealerProductsBean = new DealerProductsBean();
                    dealerProductsBean.saleType = "2";
                    dealerProductsBean.type = "1";
                    dealerProductsBean.totalCartNum = shopCartBean.data.confirmPopInfo.totalDealerCartNum;
                    dealerProductsBean.products = shopCartBean.data.confirmPopInfo.dealerProducts.get(i).products;
                    dealerProductsBean.popCartNum = shopCartBean.data.confirmPopInfo.dealerProducts.get(i).popCartNum;
                    dealerProductsBean.seller = shopCartBean.data.confirmPopInfo.dealerProducts.get(i).seller;
                    dealerProductsBeanList.add(dealerProductsBean);
                }
            }
            //微商
            if (shopCartBean.data.confirmPopInfo.agentProducts != null &&
                    shopCartBean.data.confirmPopInfo.agentProducts.size() > 0) {
                for (int i = 0; i < shopCartBean.data.confirmPopInfo.agentProducts.size(); i++) {
                    DealerProductsBean dealerProductsBean = new DealerProductsBean();
                    dealerProductsBean.saleType = "3";
                    dealerProductsBean.type = "2";
                    dealerProductsBean.totalCartNum = shopCartBean.data.confirmPopInfo.totalAgentCartNum;
                    dealerProductsBean.products = shopCartBean.data.confirmPopInfo.agentProducts.get(i).products;
                    dealerProductsBean.popCartNum = shopCartBean.data.confirmPopInfo.agentProducts.get(i).popCartNum;
                    dealerProductsBean.seller = shopCartBean.data.confirmPopInfo.agentProducts.get(i).seller;
                    dealerProductsBeanList.add(dealerProductsBean);
                }
            }
            //普通
            if (shopCartBean.data.confirmPopInfo.normalProducts != null &&
                    shopCartBean.data.confirmPopInfo.normalProducts.size() > 0) {
                for (int i = 0; i < shopCartBean.data.confirmPopInfo.normalProducts.size(); i++) {
                    DealerProductsBean dealerProductsBean = new DealerProductsBean();
                    dealerProductsBean.saleType = "1";
                    if (i == 0) {
                        dealerProductsBean.type = "3";
                    } else {
                        dealerProductsBean.type = "4";
                    }
                    dealerProductsBean.totalCartNum = shopCartBean.data.confirmPopInfo.totalNormalCartNum;
                    dealerProductsBean.products = shopCartBean.data.confirmPopInfo.normalProducts.get(i).products;
                    dealerProductsBean.popCartNum = shopCartBean.data.confirmPopInfo.normalProducts.get(i).popCartNum;
                    dealerProductsBean.seller = shopCartBean.data.confirmPopInfo.normalProducts.get(i).seller;
                    dealerProductsBeanList.add(dealerProductsBean);
                }
            }
        }
        return dealerProductsBeanList;
    }

    /**
     * 获取结算弹窗勾选商品id & 数量
     */
    String getDialogBuyProduct(List<DealerProductsBean> dealerProductsBeans) {
        JSONObject buyProductsInfo = new JSONObject();
        try {
            if (dealerProductsBeans != null && dealerProductsBeans.size() > 0) {
                for (int i = 0; i < dealerProductsBeans.size(); i++) {
                    if (dealerProductsBeans.get(i).isChecked){
                        for (int j = 0; j < dealerProductsBeans.get(i).products.size(); j++) {
                            buyProductsInfo.put(dealerProductsBeans.get(i).products.get(j).productId,
                                    Integer.parseInt(dealerProductsBeans.get(i).products.get(j).num));
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return buyProductsInfo.toString();
    }

    /**
     * 判断是否选中
     */
    boolean isChecked(List<DealerProductsBean> dealerProductsBeans) {
        boolean isChecked = false;
        if (dealerProductsBeans != null && dealerProductsBeans.size() > 0) {
            for (int i = 0; i < dealerProductsBeans.size(); i++) {
                if (dealerProductsBeans.get(i).isChecked){
                    isChecked = true;
                }
            }
        }
        return isChecked;
    }
}
